/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.smarttmt.smartmanager.controller;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import org.smarttmt.smartmanager.controller.exceptions.NonexistentEntityException;
import org.smarttmt.smartmanager.controller.exceptions.PreexistingEntityException;
import org.smarttmt.smartmanager.controller.exceptions.RollbackFailureException;
import org.smarttmt.smartmanager.entity.Objeprod;
import org.smarttmt.smartmanager.entity.Causrequ;
import org.smarttmt.smartmanager.entity.Solucion;

/**
 *
 * @author nicolas
 */
public class SolucionJpaController implements Serializable {

    public SolucionJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Solucion solucion) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Objeprod objeprod = solucion.getObjeprod();
            if (objeprod != null) {
                objeprod = em.getReference(objeprod.getClass(), objeprod.getObjeprodPK());
                solucion.setObjeprod(objeprod);
            }
            Causrequ solucade = solucion.getSolucade();
            if (solucade != null) {
                solucade = em.getReference(solucade.getClass(), solucade.getCarecodi());
                solucion.setSolucade(solucade);
            }
            em.persist(solucion);
            if (objeprod != null) {
                objeprod.getSolucionList().add(solucion);
                objeprod = em.merge(objeprod);
            }
            if (solucade != null) {
                solucade.getSolucionList().add(solucion);
                solucade = em.merge(solucade);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findSolucion(solucion.getSolucodi()) != null) {
                throw new PreexistingEntityException("Solucion " + solucion + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Solucion solucion) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Solucion persistentSolucion = em.find(Solucion.class, solucion.getSolucodi());
            Objeprod objeprodOld = persistentSolucion.getObjeprod();
            Objeprod objeprodNew = solucion.getObjeprod();
            Causrequ solucadeOld = persistentSolucion.getSolucade();
            Causrequ solucadeNew = solucion.getSolucade();
            if (objeprodNew != null) {
                objeprodNew = em.getReference(objeprodNew.getClass(), objeprodNew.getObjeprodPK());
                solucion.setObjeprod(objeprodNew);
            }
            if (solucadeNew != null) {
                solucadeNew = em.getReference(solucadeNew.getClass(), solucadeNew.getCarecodi());
                solucion.setSolucade(solucadeNew);
            }
            solucion = em.merge(solucion);
            if (objeprodOld != null && !objeprodOld.equals(objeprodNew)) {
                objeprodOld.getSolucionList().remove(solucion);
                objeprodOld = em.merge(objeprodOld);
            }
            if (objeprodNew != null && !objeprodNew.equals(objeprodOld)) {
                objeprodNew.getSolucionList().add(solucion);
                objeprodNew = em.merge(objeprodNew);
            }
            if (solucadeOld != null && !solucadeOld.equals(solucadeNew)) {
                solucadeOld.getSolucionList().remove(solucion);
                solucadeOld = em.merge(solucadeOld);
            }
            if (solucadeNew != null && !solucadeNew.equals(solucadeOld)) {
                solucadeNew.getSolucionList().add(solucion);
                solucadeNew = em.merge(solucadeNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = solucion.getSolucodi();
                if (findSolucion(id) == null) {
                    throw new NonexistentEntityException("The solucion with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Solucion solucion;
            try {
                solucion = em.getReference(Solucion.class, id);
                solucion.getSolucodi();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The solucion with id " + id + " no longer exists.", enfe);
            }
            Objeprod objeprod = solucion.getObjeprod();
            if (objeprod != null) {
                objeprod.getSolucionList().remove(solucion);
                objeprod = em.merge(objeprod);
            }
            Causrequ solucade = solucion.getSolucade();
            if (solucade != null) {
                solucade.getSolucionList().remove(solucion);
                solucade = em.merge(solucade);
            }
            em.remove(solucion);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Solucion> findSolucionEntities() {
        return findSolucionEntities(true, -1, -1);
    }

    public List<Solucion> findSolucionEntities(int maxResults, int firstResult) {
        return findSolucionEntities(false, maxResults, firstResult);
    }

    private List<Solucion> findSolucionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Solucion.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Solucion findSolucion(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Solucion.class, id);
        } finally {
            em.close();
        }
    }

    public int getSolucionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Solucion> rt = cq.from(Solucion.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
